<template>
  <page-header-wrapper>
    <global-search :loading="searchLoading" :list="searchList" @search="handleSearch" @reset="handleReset">
    </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: true }"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <span slot="time" slot-scope="text">{{ text | moment }}</span>
        <ellipsis :length="46" slot="ellipsis" slot-scope="text">{{ text }}</ellipsis>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleAction(record, 'detail')">查看</y-button>
          <a-popconfirm placement="left" title="确定要腾退该房屋吗？" @confirm="handleAction(record, 'out')">
            <y-button type="link" class="text-error">房屋腾退</y-button>
          </a-popconfirm>
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getNoticeSignOut, postUpdateNoticeSignOut } from '@/api/followUpManagement'
import { getHouseTypeColor } from '@/api/common'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
  },
  data() {
    return {
      searchLoading: false,
      searchList: [
        {
          label: '合同编号',
          fields: 'pactNo',
          type: 'input',
          props: {
            placeholder: '请输入合同编号',
          },
        },
        {
          label: '房屋状态',
          fields: 'fwzt',
          type: 'select',
          props: {
            placeholder: '请选择房屋状态',
            options: [],
          },
        },
        {
          label: '栋',
          fields: 'roof',
          type: 'input',
          props: {
            placeholder: '请输入栋好',
          },
        },
        {
          label: '单元',
          fields: 'danyuan',
          type: 'input',
          props: {
            placeholder: '请输入单元号',
          },
        },
      ],
      queryParam: {
        pactNo: '',
        fwzt: '',
        roof: '',
        danyuan: '',
        DeptNO: this.$store.getters.userInfo.DeptNO,
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNO',
        },
        {
          title: '合同编号',
          align: 'center',
          dataIndex: 'PactNo',
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'Proposer',
        },
        {
          title: '身份证',
          align: 'center',
          dataIndex: 'IDcard',
        },
        {
          title: '电话号码',
          align: 'center',
          dataIndex: 'Phone',
        },
        {
          title: '房屋地址',
          align: 'center',
          dataIndex: 'ESitesSelect',
          scopedSlots: { customRender: 'ellipsis' },
        },
        {
          title: '房屋状态',
          align: 'center',
          dataIndex: 'fwzt',
          customRender: (text) => {
            const houseType = this.searchList[1].props.options.find((e) => e.value === text)
            return <span style={{ color: houseType ? houseType.color : 'rgba(0, 0, 0, 0.65)' }}>{text}</span>
          },
        },
        {
          title: '操作',
          align: 'center',
          width: 140,
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getNoticeSignOut(requestParameters).then((res) => res)
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.searchLoading = true
      getHouseTypeColor()
        .then((res) => {
          this.searchList[1].props.options = res.map((e) => {
            return {
              value: e.PMNAME,
              label: `${e.PMNAME}${e.PMBREAK ? `(${e.PMBREAK})` : ''}`,
              color: e.PMCOLOR,
            }
          })
        })
        .finally(() => {
          this.searchLoading = false
        })
    },
    handleSearch(values) {
      Object.assign(this.queryParam, values)
      this.tableRefresh(true)
    },
    handleReset() {
      for (const key in this.queryParam) {
        this.queryParam[key] = ''
      }
      this.tableRefresh(true)
    },
    tableRefresh(refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleAction(record, type) {
      this.editData = record
      if (type === 'detail') {
        this.$router.push({
          name: 'followUpManagementHouseVacatedDetail',
          params: {
            id: record.ID,
          },
        })
      } else if (type === 'out') {
        this.$loading.show({
          tip: '提交中，请稍后...',
        })
        postUpdateNoticeSignOut({
          userid: this.$store.getters.userid,
          id: record.ESites,
          packId: record.ID,
        })
          .then((res) => {
            this.$message.success('房屋腾退成功！')
            this.tableRefresh()
          })
          .finally(() => {
            this.$loading.hide()
          })
      }
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-table td {
  white-space: nowrap;
}
</style>
